package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
	"go.uber.org/zap"
	"img_analyse/middleware/Cors"
	logger "img_analyse/middleware/Logger"
	"img_analyse/user/api/internal/config"
	"img_analyse/user/api/internal/handler"
	"img_analyse/user/api/internal/svc"
)

var configFile = flag.String("f", "etc/users.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	// 初始化日志
	logger.InitLogger("logs/service.log", "info")
	defer logger.Sync()

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	// 添加中间件 - 注意顺序很重要！
	server.Use(logger.RequestLogger) // 请求日志
	server.Use(Cors.CorsMiddleware)  // CORS

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	logger.Logger.Info("Starting server",
		zap.String("host", c.Host),
		zap.Int("port", c.Port),
	)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
